<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-col :span="1">
            <el-button type="primary" size="small" @click="setRights(1, '0')">添加权限</el-button>
          </el-col>
        </el-row>
        <!-- 表格
          支持树类型的数据的显示。当 row 中包含 children 字段时，被视为树形数据。渲染树形数据时，必须要指定 row-key
         -->
        <el-table :data="permissionList" row-key="id" border stripe style="margin-top:20px">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default=" {row} ">
              <!-- <pre>
                {{ row }}
              </pre> -->
              <el-button v-if="row.type === 1" type="primary" size="small" @click="setRights(2, row.id )">添加权限点</el-button>
              <el-button type="warning" size="small" @click="editRights(row.id)">查看</el-button>
              <el-button type="success" size="small" @click="delRights(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog :visible="showDialog" :title="title" @close="closeDialog">
          <!-- 表单内容 -->
          <el-form label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right;">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="handleSubmit">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { reqGetPermissionList, reqAddPermission, reqGetPermissionDetail, reqUpdatePermission, reqDelPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [], // 所有权限列表
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      title: '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限列表
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.permissionList = tranListToTreeData(data, '0')
    },
    // 显示新增权限点的对话框
    setRights(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
      this.title = '新增权限'
    },
    // 提交新增/编辑权限
    async handleSubmit() {
      if (this.formData.id) {
        // 编辑
        await reqUpdatePermission(this.formData)
      } else {
        // 新增
        // 1. 新增权限发送请求
        await reqAddPermission(this.formData)
      }
      // 2. 关闭对话框
      this.showDialog = false
      // 3. 重新渲染
      this.getPermissionList()
    },
    // 点击查看展示对话框
    async editRights(id) {
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.title = '查看权限'
    },
    // 关闭对话框
    closeDialog() {
      // 1. 设置布尔值为false
      this.showDialog = false
      // 2. 重置表单
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 删除权限
    async delRights(id) {
      // 1. 确认消息框提示
      const str = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {})
      // 2. 发送删除的请求
      if (str !== 'confirm') return this.$message.info('已取消操作')
      await reqDelPermission(id)
      // 3. 重新渲染
      this.getPermissionList()
    }
  }
}
</script>

<style>

</style>
